<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: forumindex.php 27451 2012-02-01 05:48:47Z monkey $
     *      提交文章评论
     */
    if (!defined('IN_MOBILE_API')) {
        exit('Access Denied');
    }

    include_once 'forum.php';

    class cloud_api {

        function common() {
            
        }

        function output() {
            if(empty($_GET['appid'])){
                //正常返回
                $variable = array(
                    'code'=>1001,
                    'messagestr' => '缺少参数：appid!',
                );
                cloud_core::result (cloud_core::variable ($variable));
            }
	     $count = empty($_GET['count'])?0:$_GET['count']; 
             //1,获取正常状态的uid
            if(empty($_GET['udid'])){
                $data = C::t('#cloud#cloud_ios_push')->fetch_all_by_caid($_GET['appid']);
            }else{
                $data = C::t('#cloud#cloud_ios_push')->fetch_push_all_by_udid($_GET['udid'],$_GET['appid']);
            }
  
                //2,循环未读消息进行推送
                if(!empty($data)){
                    foreach ($data as $key=>$val) {
                       $token = $val['token'];
                       $dev  = $val['dev']; 
                       self::push_message($token,$count,$dev,$_GET['appid']);
                    }
                }
//             exit;
            //正常返回
              $variable = array(
                  'messagestr' => '',
              );
              cloud_core::result (cloud_core::variable ($variable));
        }
        function push_message($token,$count,$dev,$caid){
            // 得到deviceToken
            $deviceToken = $token;
            // 密钥:
            $passphrase = 'youdao';
            // 要推送的消息:
            $sound = 'default';
            $message = empty($_GET['message'])?'测试推送数据接口':$_GET['message'];
			 
            $ctx = stream_context_create();
            if($dev==0){
//                echo 'aaaaa0:'.$dev.'<br>';
                stream_context_set_option($ctx, 'ssl', 'local_cert', 'source/plugin/cloud/pushPem/product/'.$caid.'.pem');
                stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
                // 打开一个连接到apns服务器
                $fp = stream_socket_client(  'ssl://gateway.push.apple.com:2195', $err,  $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
            }else{
//                echo 'aaaaa1:'.$dev.'<br>';
                stream_context_set_option($ctx, 'ssl', 'local_cert', 'source/plugin/cloud/pushPem/develop/'.$caid.'.pem');
                stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
                // 打开一个连接到apns服务器
                $fp = stream_socket_client(  'ssl://gateway.sandbox.push.apple.com:2195', $err,  $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
            }

//             if (!$fp)
//               exit("Failed to connect: <br>$err<br>$errstr<br>" . PHP_EOL);
                    
            $body['aps'] = array(
                    'alert' => $message,
                    'sound' => $sound,
                    "badge" => $count
                    );
            // 转换json
            $payload = json_encode($body);
            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
            $result = fwrite($fp, $msg, strlen($msg));
//            echo '$result:'.$result.'<br>';
            return $result ? true:false;  
            fclose($fp);
        }
    }

?>
